﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using UnityEngine;
using Verse;

namespace ARMIR
{
    public class CompShield_sharedShield : CompShield {
        public new CompProperties_sharedShield Props => (CompProperties_sharedShield)props;

        public int drawDamageTrans = 0;
        public float lineWidth = 0.1f;

        public override void CompDrawWornExtras()
        {
            Draw();
        }
        public override void PostDraw()
        {
            Draw();
        }

        public override bool CompAllowVerbCast(Verb verb)
        {
            return true; // 允许所有武器（包括远程）
        }

        private void Break()
        {

        }

        public override void PostPreApplyDamage(ref DamageInfo dinfo, out bool absorbed)
        {
            absorbed = false;
            if (ShieldState != ShieldState.Active || PawnOwner == null) { 
                return;
            }
            List<Pawn> Rosmontis = PawnOwner.Map.mapPawns.AllHumanlikeSpawned.Where(p => p.health.hediffSet.HasHediff(DefDatabase<HediffDef>.GetNamed("ARMIR_OriginiumBrainStem"))
            && p.Position.DistanceTo(PawnOwner.Position) < 8
            && p.Faction == PawnOwner.Faction).ToList();
            if (Rosmontis.Count > 0) {
                // 获取Pawn身上mindshield头环
                Apparel mindShieldApparel = Rosmontis[0].apparel.WornApparel.FirstOrDefault(apparel =>
                    apparel.def.defName == "ARMIR_mindShield");

                if (mindShieldApparel != null)
                {
                    // 获取shield组件
                    CompShield_MindShield shieldComp = mindShieldApparel.GetComp<CompShield_MindShield>();
                    if (shieldComp != null)
                    {
                        // 使用shield组件
                        //Log.Message($"找到mindshield，护盾能量：{shieldComp.Energy}");
                        if (shieldComp.Energy > 0) {
                            Rosmontis[0].TakeDamage(dinfo);
                            drawDamageTrans += 50;
                        }else { 
                            return;
                        }
                        absorbed = true;
                    }
                    else { 
                        return;
                    }
                }else { 
                    return;
                }
            }
            else { 
                return;
            }
            
            //PawnOwner 获得pawn
        }
        private void Draw()
        {
            List<Pawn> Rosmontis = PawnOwner.Map.mapPawns.AllHumanlikeSpawned.Where(p => p.health.hediffSet.HasHediff(DefDatabase<HediffDef>.GetNamed("ARMIR_OriginiumBrainStem"))
            && p.Position.DistanceTo(PawnOwner.Position) < 8
            && p.Faction == PawnOwner.Faction).ToList();
            if (Rosmontis.Count > 0 && Rosmontis != null) {
                if (Rosmontis[0] != null) {
                    Material whiteMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.302f, 0.941f, 0.914f, 0.1f));
                    float altitude = AltitudeLayer.MetaOverlays.AltitudeFor();

                    if (UnityEngine.Random.value > 0.8) { lineWidth = UnityEngine.Random.value; }
                    Vector3 pointA = new Vector3(PawnOwner.Position.ToVector3().x + 0.5f, altitude, PawnOwner.Position.ToVector3().z + 0.5f);
                    Vector3 pointB = new Vector3(Rosmontis[0].Position.ToVector3().x + 0.5f, altitude, Rosmontis[0].Position.ToVector3().z + 0.5f);
                    if (drawDamageTrans > 0)
                    {
                        Material whitMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.302f, 0.941f, 0.914f, 0.4f));

                        GenDraw.DrawLineBetween(pointA,pointB, whitMat, lineWidth / 2);

                        drawDamageTrans--;
                    }
                    else
                    {

                        GenDraw.DrawLineBetween(pointA,pointB, whiteMat, 0.1f);
                    }
                }
            }
        }
    }
}
